#!/bin/bash

. ./path.sh || exit 1;
cd ..

export CUDA_VISIBLE_DEVICES="0,1,2,3,4,5,6,7"
stage=5 # start from 0 if you need to start from data_list preparation
stop_stage=5

dir=
train_config=
decode_checkpoint=
#decode_checkpoint_name="0pt"
decode_checkpoint_name=
gpu_id=
decode_modes="salmonn_decode"
test_data_dir="/home/work_nfs8/xlgeng/data/scp_test"
#test_sets=("aishell"  "aishell2" "assistant" "huawei_accent" "speechio_0"  "speechio_1" "speechio_2" "speechio_3" "huawei_noisy"  "huawei_real" "noise_103" "noise_107" "speechio_4"  "test_meeting" "test_net" "xiaotaicai" )
test_sets="aishell--aishell2--ggg"
test_set_suffix=""
prompt_id=0
. tools/parse_options.sh || exit 1;

test_sets=$(echo "$test_sets" | sed 's/--/ /g')
echo "开始打印主要变量，这些变量有命令行传入"
echo "dir=$dir"
echo "train_config=$train_config"
echo "decode_checkpoint=$decode_checkpoint"
echo "decode_checkpoint_name=$decode_checkpoint_name"
echo "gpu_id=$gpu_id"
echo "stage=$stage"
echo "stop_stage=$stop_stage"
echo "test_sets=$test_sets"
echo "test_set_suffix=$test_set_suffix"
echo "prompt_id=$prompt_id"
#for test_set in "${test_sets[@]}"; do
for test_set in $test_sets; do
{
    echo "prepare test this dataset: $test_set"
}
done
wait
set -e
set -u
set -o pipefail


if [ ${stage} -le 5 ] && [ ${stop_stage} -ge 5 ]; then
  ctc_weight=0.5
  for test_set in $test_sets; do
  {
    echo "test this dataset: $test_set"
    test_dir=$dir/test_${decode_checkpoint_name}_${test_set_suffix}/${test_set}
#    wer_path=$test_dir/wer
#    if [ -e "$wer_path" ]; then
#      echo "$wer_path 文件已存在，跳过对该数据集的推理"
#      continue
#    fi
    mkdir -p $test_dir
    export CUDA_VISIBLE_DEVICES=$gpu_id
    python wenet/bin/recognize.py --gpu $gpu_id \
      --modes $decode_modes \
      --config $dir/train.yaml \
      --data_type raw \
      --test_data $test_data_dir/$test_set/data.list \
      --checkpoint $decode_checkpoint \
      --beam_size 10 \
      --batch_size 1 \
      --penalty 0.0 \
      --result_dir $test_dir \
      --ctc_weight $ctc_weight \
      --prompt_id $prompt_id

#    python tools/compute-wer.py --char=1 --v=1 \
#      data_list/test/$test_set/text $test_dir/text_hyp > $test_dir/wer
    echo "$test_set has been decoded!"
    python tools/compute-wer.py --char=1 --v=1 \
      $test_data_dir/$test_set/text $test_dir/text > $test_dir/wer
  }
  done
  wait

fi
if [ ${stage} -le 6 ] && [ ${stop_stage} -ge 6 ]; then
  ctc_weight=0.5
  for test_set in "${test_sets[@]}"; do
  {
    echo "compute wer this dataset: $test_set"
    test_dir=$dir/test_${decode_checkpoint_name}_${test_set_suffix}/${test_set}
    python tools/compute-wer.py --char=1 --v=1 \
      $test_data_dir/$test_set/text $test_dir/text > $test_dir/wer
    echo "$test_set has been decoded!"
  }
  done
  wait

fi
